package com.qf.springbootdemo03.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.springbootdemo03.mapper.IPermissionMapper;
import com.qf.springbootdemo03.mapper.IRoleMapper;
import com.qf.springbootdemo03.mapper.IUserInfoMapper;
import com.qf.springbootdemo03.pojo.UserInfo;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author q
 * @version V1.0
 * @date 2021/11/25
 **/
@Service
public class UserService implements UserDetailsService {

    @Autowired
    private IUserInfoMapper userInfoMapper;

    @Autowired
    private IRoleMapper roleMapper;

    @Autowired
    private IPermissionMapper permissionMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        //根据用户名查询用户信息
        QueryWrapper wrapper = new QueryWrapper<UserInfo>();
        wrapper.eq("username", username);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);

        if (null == userInfo){
            throw new UsernameNotFoundException("用户不存在~");
        }

        //查询角色列表，
        List<String> roleNames = roleMapper.findRoleNamesByUserId(userInfo.getId());
        String[] roles = new String[roleNames.size()];

        //查询权限名称
        List<String> allPermissionNames = new ArrayList<>();

        for (int i = 0; i < roles.length; i++) {
            roles[i] = "ROLE_" + roleNames.get(i);
            List<String> permissionNames = permissionMapper.findPermissionNameByRoleName(roleNames.get(i));
            for (int j = 0; j < permissionNames.size(); j++) {
                //System.out.println(roleNames.get(i));
                //System.out.println(permissionNames.get(j));
                //System.out.println(roleNames.get(i) + "_" + permissionNames.get(j));
                allPermissionNames.add(roleNames.get(i) + "_" + permissionNames.get(j));
            }
        }

        //包装成 GrantedAuthority
        List<GrantedAuthority> authorities = AuthorityUtils.createAuthorityList(roles);
        authorities.addAll(AuthorityUtils.createAuthorityList(allPermissionNames.toArray(new String[0])));

        System.out.println("=======角色=======");
        for (String role : roles) {
            System.out.println(role);
        }
        System.out.println("==================");

        System.out.println("-------权限-------");
        for (String allPermissionName : allPermissionNames) {
            System.out.println(allPermissionName);
        }
        System.out.println("------------------");

        System.out.println("~~~ authorities ~~~");
        for (GrantedAuthority authority : authorities) {
            System.out.println(authority);
        }
        System.out.println("~~~~~~~~~~~~~~~~~~");

        //Collection<? extends GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("");
        return new User(userInfo.getUsername(), userInfo.getPassword(), authorities);
    }
}
